package com.example.helloandroid;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.SystemClock;
import android.widget.TextView;

public class HelloAndroid extends Activity {
	
	private TextView tekst, data;
	BufferedWriter g_out;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        tekst = (TextView)findViewById(R.id.tekst);
        data = (TextView)findViewById(R.id.data);

        SensorManager sManager = (SensorManager)getSystemService( Context.SENSOR_SERVICE ); 
        
        List<Sensor> liste = sManager.getSensorList(Sensor.TYPE_ALL);
        for(int a=0;a<liste.size();a++) {
        	tekst.append("\nSensor "+a+": "+liste.get(a).getName());
        } 
        
        sManager.registerListener(sensorEventListener, liste.get(0), 3); 
        
        try { // catches IOException below 
            File root = Environment.getExternalStorageDirectory();
            if (root.canWrite()){
	            File f = new File(root, "test.txt");
	            FileWriter fw = new FileWriter(f);
	            BufferedWriter out = new BufferedWriter(fw);
	            g_out = out;
	            out.write("T[ms]\tdT[ms]\tgx\tgy\tgz\n");
	            //out.close();
            }
            
            tekst.append(String.format("\nFile creation passed at %d milliseconds",SystemClock.uptimeMillis()));
            
       } catch (IOException ioe) {
            //ioe.printStackTrace();
            tekst.append("file creation failed\n");
       }  
    }
    

    private SensorEventListener sensorEventListener = new SensorEventListener() {
    	
    private long[] dMilli = {0,0,0};
    private long cnt=0;
    
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }

        public void onSensorChanged(SensorEvent e) {
          synchronized (this) {
        	  //dMilli = new long[3];
        	  dMilli[1] = dMilli[0];
        	  dMilli[0] = SystemClock.uptimeMillis();
              //System.out.println("Sensor event: "+e.values[0]+" "+e.values[1]+" "+e.values[2]+" ");
              
              try {
				//g_out.append("Hello world\n");
            	  
            	g_out.append(String.format("%d\t%d\t%f\t%f\t%f\n",dMilli[0],(dMilli[0]-dMilli[1]),e.values[0],e.values[1],e.values[2]));
            	
				if( ( dMilli[0] - dMilli[2] ) > 500)
				{
					dMilli[2] = dMilli[0];
					data.setText(String.format("Sensor changed @ %d milliseconds\n",dMilli[0]));
					data.append(String.format("delta t %d milliseconds\n",dMilli[0]-dMilli[1]));
					data.append(String.format("x %f\n",e.values[0]));
					data.append(String.format("y %f\n",e.values[1]));
					data.append(String.format("z %f\n",e.values[2]));
					data.append(String.format("Write passed; cnt: %d",cnt));
					g_out.flush();
					
				}
            	
            	if(cnt==65000)
				{
					g_out.flush();
					g_out.close();
				}
				
				cnt++;
				
				
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				data.append("Write FAILED");
			}
              
           
          }      
        }

          
      }; 
    

   
//    public void onSensorChanged(int sensor, float[] values)
//    {
// 	   tekst.setText(String.format("Sensor changed @ %d milliseconds",SystemClock.uptimeMillis()));    	   
//    }
    
}